﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HOOMT
{
    class TreeNodeGenerator
    {
        MainFormDAO mdbo = new MainFormDAO();
        MainForm mainForm = null;

        public TreeNodeGenerator(MainForm mainForm)
        {
            this.mainForm = mainForm;
        }

        private TreeNode GenerateFormNode(OriginalModelForm form)
        {
        //    Console.WriteLine("GenerateFormNode");
            TreeNode node = new TreeNode(mdbo.GetSimpleType(form.GetType().ToString()) + " id:" 
                + (form.id).Substring(0,5)+"...");
            node.Tag = form;
          //  Console.WriteLine(node.Tag);
            if (form.diagramList != null)
            {
                foreach (OriginalDiagram diagram in form.diagramList)
                {
                    node.Nodes.Add(this.GenerateDiagramNode(diagram));
                    
                }
            }
            return node;
        }
        private TreeNode GenerateDiagramNode(OriginalDiagram diagram)
        {
      //      Console.WriteLine("GenerateDiagramNode");
            TreeNode node = new TreeNode(diagram.Name);
            node.Tag = diagram;
       //     Console.WriteLine(node.Tag);
            if (diagram is HighOrderObjectDiagram)
            {
                OriginalModelForm childForm = ((HighOrderObjectDiagram)diagram).NewModelForm;
                if (childForm != null)
                {
                    node.Nodes.Add(this.GenerateFormNode(childForm));
                }
            }
            else if (diagram is HighStateDiagram)
            {
                OriginalModelForm childForm = ((HighStateDiagram)diagram).NewModelForm;
                if (childForm != null)
                {
                    node.Nodes.Add(this.GenerateFormNode(childForm));
                }
            }
            else if (diagram is InformationSourceDiagram)
            {
                OriginalModelForm childForm = ((InformationSourceDiagram)diagram).NewModelForm;
                if (childForm != null)
                {
                    node.Nodes.Add(this.GenerateFormNode(childForm));
                }

            }
            return node;
        }

        public TreeNode GenerateMainNode()
        {
            TreeNode node = new TreeNode();
            node.Tag = this.mainForm;
            if (this.mainForm.projectName == null)
            {
                node.Text = "Untitled";
            }
            else
            {
                node.Text = this.mainForm.projectName;
            }
        //    Console.WriteLine("-----"+mdbo.GetNonSubFormList(mainForm).Count);
            foreach (OriginalModelForm form in mdbo.GetNonSubFormList(mainForm))
            {
                node.Nodes.Add(this.GenerateFormNode(form));
            }

            node.ExpandAll();
            return node;

        }
    }
}
